<?php
namespace app\index\controller;
use think\Controller;
class Common extends Controller
{
    public function _initialize()
    {
        parent::_initialize();
        // 获取访问栏目顶级标识
        $controller=request()->controller();
        $curr=$this->getCurrType($controller);
        // 获取网站配置信息
        $config=db('config')->field('config')->find();
        $config=json_decode($config['config'],true);
        if (!isset($config['state'])) {
            $config['state']=0;
        }
        $this->checkclose($config['state'],$config['closeinfo']);
        $cate=$this->getCate();
        $this->getchild(0);
        $this->assign([
            'cate'   =>$cate,
            'curr'   =>$curr,
            'config' =>$config,
        ]);
    }
    // 获取栏目
    private function getCate(){
        $cate=db('category')->where(array('pid'=>0,'isshow'=>1))->field('id,cate_name,type')->order('sort DESC,id ASC')->select();
        foreach ($cate as $k => $v) {
            $child=db('category')->field('id,cate_name,type')->where(array('pid'=>$v['id'],'isshow'=>1))->select();
            $cate[$k]['child']=$child;
        }
        return $cate;
    }
    // 获取栏目类型
    private function getCurrType($controller){
       switch($controller){
            case 'Index':
                return 0;
             case 'Page':
                return 1;
            case 'Works':
                return 2;
             case 'Model':
                return 3;
            case 'Scene':
                return 4;
            case 'News':
                return 5;
            case 'Contactus':
                return 7;
            default:
                return 0;
       }
    }
    /**
     * 检查网站是否关闭
     */
    public function checkclose($state,$info="网站关闭"){
        if ($state!=1) {
            header('Content-Type:text/html;charset=utf-8');
            echo $info;
            exit;
        }
    }
    // 获取子分类
    public function getchild($id=0){
        $child=db('category')->field('id,cate_name,type')->where('pid',$id)->select();
        return $child;
    }
   
    /**
     * 面包屑导航
     * 首页>关于我们>企业简介
     * param $id  当前栏目id
     */
    public function getPos($id){
        static $arr=array();
        if (empty($arr)) {
            $cate=db('category')->field('id,cate_name,type,pid')->find($id);
            $arr[]=$cate;
        }
        $data=db('category')->field('id,cate_name,type,pid')->select();
        $cate=db('category')->field('id,cate_name,type,pid')->find($id);
        foreach ($data as $k => $v) {
            if ($cate['pid']==$v['id']) {
                $arr[]=$v;
                $this->getPos($v['id']);
            }
        }
        return array_reverse($arr);
        
    }
}
